package com.app.newcsc.fragment.my.level;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;

import com.app.newcsc.app.AppApplication;
import com.app.newcsc.data.DemoRepository;
import com.app.newcsc.entity.LevelInfoEntity;
import com.app.newcsc.entity.UpGradeEntity;
import com.app.newcsc.entity.UserEntity;
import com.app.newcsc.http.response.ApiException;
import com.app.newcsc.utils.TextUtil;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class LevelUpViewModel extends BaseViewModel<DemoRepository> {

    public SingleLiveEvent<UserEntity> mUserInfoEntity = new SingleLiveEvent<>();
    public SingleLiveEvent<LevelInfoEntity> mLevelInfoEntity = new SingleLiveEvent<>();
    public SingleLiveEvent<String> successStr = new SingleLiveEvent<>();

    public LevelUpViewModel(@NonNull Application application, DemoRepository model) {
        super(application, model);
    }

    public void baseUser() {
        Log.e("TAG", "==========baseUser=============");
        model.baseUser()
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse<UserEntity>>() {
                    @Override
                    public void onNext(BaseResponse<UserEntity> response) {
                        Log.e("TAG", "================baseUser===onNext====");
                        if (null != response && null != response.getData()) {
                            mUserInfoEntity.postValue(response.getData());
                            AppApplication.ad_exchange_level = response.getData().ad_exchange_level;
                            ((AppApplication) AppApplication.getInstance()).setUserEntity(response.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG", "================baseUser===onError====" + throwable.getMessage());
                        if (throwable instanceof ResponseThrowable) {
                            String msg = ApiException.getShowmessage(((ResponseThrowable) throwable).errorBody);
                            int code = ((ResponseThrowable) throwable).code;
                            if ((!TextUtil.isEmpty(msg) && msg.contains("被禁")) || code == 403) {
                                if (TextUtil.isEmpty(AppApplication.userJinYongMsg)) {
                                    AppApplication.userJinYongMsg = msg;
                                }
                                ToastUtils.showShortSafe(msg);
                            } else {
                                if (!TextUtil.isEmpty(AppApplication.userJinYongMsg)) {
                                    AppApplication.userJinYongMsg = "";
                                }
                            }

                        }
                        dismissDialog();

                    }

                    @Override
                    public void onComplete() {
                        Log.e("TAG", "================baseUser===onComplete====");
                        if (!TextUtil.isEmpty(AppApplication.userJinYongMsg)) {
                            AppApplication.userJinYongMsg = "";
                        }
                        dismissDialog();
                    }
                });
    }

    public void levelInfo() {
        model.levelInfo()
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<LevelInfoEntity>() {
                    @Override
                    public void onNext(LevelInfoEntity response) {
                        mLevelInfoEntity.postValue(response);
                    }

                    @Override
                    public void onError(Throwable throwable) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void upGrade() {
        model.upGrade()
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<UpGradeEntity>() {
                    @Override
                    public void onNext(UpGradeEntity entity) {
                        if (entity.getUpgrade() == 1) {
                            successStr.postValue("1");
                        }else {
                            successStr.postValue("2");
                            ToastUtils.showShort("兑换失败，请稍后再试");
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        successStr.postValue("2");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
